1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.cache;
18
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableMap;
21 import com.google.common.collect.Lists;
22 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture;
24
25 import junit.framework.TestCase;
26
27 import java.util.LinkedList;
28 import java.util.Map;
29 import java.util.concurrent.Executor;
30 import java.util.concurrent.atomic.AtomicInteger;
31
32
33
34
35
36
37 public class CacheLoaderTest extends TestCase {
38
39 private static class QueuingExecutor implements Executor {
40 private LinkedList<Runnable> tasks = Lists.newLinkedList();
41
42 @Override
43 public void execute(Runnable task) {
44 tasks.add(task);
45 }
46
47 private void runNext() {
48 tasks.removeFirst().run();
49 }
50 }
51
52 public void testAsyncReload() throws Exception {
53 final AtomicInteger loadCount = new AtomicInteger();
54 final AtomicInteger reloadCount = new AtomicInteger();
55 final AtomicInteger loadAllCount = new AtomicInteger();
56
57 CacheLoader<Object, Object> baseLoader = new CacheLoader<Object, Object>() {
58 @Override
59 public Object load(Object key) {
60 loadCount.incrementAndGet();
61 return new Object();
62 }
63
64 @Override
65 public ListenableFuture<Object> reload(Object key, Object oldValue) {
66 reloadCount.incrementAndGet();
67 return Futures.immediateFuture(new Object());
68 }
69
70 @Override
71 public Map<Object, Object> loadAll(Iterable<? extends Object> keys) {
72 loadAllCount.incrementAndGet();
73 return ImmutableMap.of();
74 }
75 };
76
77 assertEquals(0, loadCount.get());
78 assertEquals(0, reloadCount.get());
79 assertEquals(0, loadAllCount.get());
80
81 baseLoader.load(new Object());
82 baseLoader.reload(new Object(), new Object());
83 baseLoader.loadAll(ImmutableList.of(new Object()));
84 assertEquals(1, loadCount.get());
85 assertEquals(1, reloadCount.get());
86 assertEquals(1, loadAllCount.get());
87
88 QueuingExecutor executor = new QueuingExecutor();
89 CacheLoader<Object, Object> asyncReloader =
90 CacheLoader.asyncReloading(baseLoader, executor);
91
92 asyncReloader.load(new Object());
93 asyncReloader.reload(new Object(), new Object());
94 asyncReloader.loadAll(ImmutableList.of(new Object()));
95 assertEquals(2, loadCount.get());
96 assertEquals(1, reloadCount.get());
97 assertEquals(2, loadAllCount.get());
98
99 executor.runNext();
100 assertEquals(2, loadCount.get());
101 assertEquals(2, reloadCount.get());
102 assertEquals(2, loadAllCount.get());
103 }
104 }